// Agent technicalAnalyst in project StockMarket

/* Initial beliefs and rules */
{ include("common.asl") }

/* Initial goals */

/* Plans */
+!finishOperations : stockOpen
 <- -stockOpen;
 	!report;
.
+!finishOperations
<- 	!report;
.
+!report
<-	for(portfolio(S,X,Q))
	{
		Y=X*Q;
		.print(S," ",Y);
	}
//	.print("Performance ---------> ",Y);
	
	
.
-!finishOperations [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.

+!start
 <- .print("Hello i'm a Technical Analyst, I can forecasting the direction of prices through the study of past market data, primarily price and volume");
				joinWorkspace("ora4mas",_);
				+stockOpen;
				!execStrat;
				
//				!dispose_stock_artifacts;
.
-!start [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
   .

+!execStrat : stockOpen
<-
 				!addStock("VALE5");
				!addStock("PETR4");
				!addStock("ITUB4");
				!addStock("OGXP3");
				.wait(5000);
				!!execStrat;
.			
+!execStrat
<- .print("Operations has finished");
.

-!execStrat [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.


						
+!addStock(S) <- !discover_art(S);
						.print("Stock ",S," on focus");
						getStockPrice[S];
						getStockChange[S];
						getMma(20)[S];
						getIfr(14)[S];
						getEstocasticoRapido(14)[S];
						getObv(50)[S];
						!stopFocusArt(S);
.
/*
+!dispose_stock_artifacts     
   <- 	Stocks = ["PETR4"];
      for ( Stocks[atifactName(ArtId, S)]) {
		!stopFocusArt(ArtId);
		!disposeArtifact(ArtId);
		.print("Papel ",S," NÃO esta mais sendo acompanhado.");
     }.

-!dispose_stock_artifacts [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
   .
*/						
+stockPrice(V) [artifact_name(ArtId,Stock)]: V > 0 
<- 	.print(Stock," Stock Price: ",V);
	-price(Stock, V);
	+price(Stock, V);
.


+stockChange(V) [artifact_name(ArtId,Stock)]<- .print(Stock," Stock Change: ",V).
						
+mma(V) [artifact_name(ArtId,Stock)] : (V \== 0) 
<-	.print(Stock, " MMA == ",V)
	.

+ifr(V)  [artifact_name(ArtId,Stock)] : (V \== 0)
 <-	.print(Stock, " IFR == ",V)
.

+estocasticoRapido(V)  [artifact_name(ArtId,Stock)] : price(Stock, P)
 <-	.count(buyByK(Stock,_), DegreeOfCertainty);
	+buyBias[doc("K",Stock,DegreeOfCertainty,P)];
	+tryK;
	!tryToBuy(Stock,V,P); 
	!tryToSell(Stock,V,P); 
	.print(Stock, " K% == ",V)
 .


+obv(V) [artifact_name(ArtId,Stock)] : price(Stock, P) & (V == true)
<-	.count(buyByOBV(Stock,_), DegreeOfCertainty);
	+buyBias[doc("OBV",Stock,DegreeOfCertainty,P)];
	+tryOBV;
	!tryToBuy(Stock,V,P); 
	.print(Stock, " OBV == ",V);
.